package com.budwk.app.sqfwV2.generator.dayingRoadPdf.rw;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.dayingRoad.DaYingLuWhiteRecordPdfDTO;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.deepoove.poi.data.PictureRenderData;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-02-21
 * <p>
 * 大营路白事备案表 DaYingLuWhiteRecord
 * PDF红白喜事办理安全承诺书.docx
 */
public class RedAndWhiteSecurityGenerator extends BasePdfGenerator {
    @Override
    protected boolean pdfCreateConditionCheck(PdfGenerateParam pdfGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        JSONObject jsonObject = JSONUtil.parseObj(pdfGenerateParam.getAbstractCustomerPdfCreateField());

        paramMap.put("date", paramMap.get("fillDateStr"));

        // 图片处理
        Object signature = pdfGenerateParam.getCustomerParamMap().get("signature");
        List<String> signatureList = Arrays.asList(signature == null ? "" : signature.toString());
        String signatureName = "signature";
        List<Map<String, PictureRenderData>> signatures = CommonFormOfficeUtils.addWordPicture(signatureList, signatureName, 1.0F);
        paramMap.put(signatureName + "s", signatures);
        if (signature != null && StringUtils.isNotBlank(signature.toString())) {
            paramMap.put("createUserName", "");
        }

        // 图片处理
        String fieldName = ClassUtil.getField(DaYingLuWhiteRecordPdfDTO::getCheckedUnitLeader);
        Object img = jsonObject.get(fieldName);
        List<String> pictureUrlList = Arrays.asList(img == null ? "" : img.toString());
        List<Map<String, PictureRenderData>> pictures = CommonFormOfficeUtils.addWordPicture(pictureUrlList, fieldName, 1.0F);
        paramMap.put(fieldName + "s", pictures);
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "daYingLu/white/redAndWhiteSecurity.docx";
    }
}
